#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@author: Lénia Andrade nº 5607 

code adapted from (n°6207)Edgar Pereira's code 
and http://www.velocityreviews.com/forums/t330554-kill-a-thread-in-python.html
'''

import SimpleHTTPServer
import SocketServer
from KThread import *


class CreateHttpServer (threading.Thread):
    
    """
    Thread class with a stop() method
    """

    server = None
  
    '''
    This method is used to kill the thread (stop server)
    '''
    def stop (self):
        server.kill()
        print "HTTP server stopped"
        pass
    pass
  
    
    '''
    This method is used to start the server on a KThread
    '''
    def start (self):
        global server
        server = KThread(target=self.start_thread)
        server.start()
        pass
    pass



    '''
    This method creates the http server
    '''
    def start_thread (self):
        PORT = 8000
        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        httpd = SocketServer.TCPServer(('', PORT), Handler)        
        print "HTTP serving at port", PORT
        httpd.serve_forever()
        pass
    pass
